#include <graphics.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <windows.h>

#define PI 3.14159265358979323846

void drawBaseDashboard(float centerX, float centerY, float radius) // 表盘界面(比例化版本)
{
	ege_enable_aa(true);
// 计算比例因子 (基于原始radius=280的比例)
	const float scale = radius / 280.0f;
	
// 比例化参数
	const int sechang = static_cast<int>(20 * scale);
	const float outer_ring1 = radius + 27 * scale;
	const float outer_ring2 = radius + 20 * scale;
	const float outer_ring3 = radius + 17 * scale;
	const float inner_ring = radius - sechang;
	const float mid_ring = radius - sechang/2;
	
	cleardevice();
	setcolor(EGERGB(110, 110, 110));
// 绘制一个圆
	ege_circle(centerX, centerY, radius);
	
// ==================== 第一层外环 ====================
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillpie(centerX - outer_ring1, centerY - outer_ring1, outer_ring1 * 2, outer_ring1 * 2, 0, 360, NULL);
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring1, centerY - outer_ring1, outer_ring1 * 2, outer_ring1 * 2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring1, centerY - outer_ring1, outer_ring1 * 2, outer_ring1 * 2, 370 - i*2, 10, NULL);
	}
	
// ==================== 第二层外环 ====================
	setcolor(EGERGB(10, 10, 10));
	setfillcolor(EGERGB(10, 10, 10));
	ege_fillcircle(centerX, centerY, outer_ring2);
	
	for(int i = 0; i <= 30; i++) {
		const float r1 = outer_ring2 - 2;
		setcolor(EGERGB(80+i, 80+i, 80+i));
		setfillcolor(EGERGB(80+i, 80+i, 80+i));
		ege_fillpie(centerX - r1, centerY - r1, r1 * 2, r1 * 2, 30 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		const float r2 = outer_ring2 - 1;
		setcolor(EGERGB(80+i, 80+i, 80+i));
		setfillcolor(EGERGB(80+i, 80+i, 80+i));
		ege_fillpie(centerX - r2, centerY - r2, r2 * 2, r2 * 2, 160 - i*2, 10, NULL);
	}
	
// ==================== 第三层外环 ====================
	setcolor(EGERGB(20, 20, 20));
	setfillcolor(EGERGB(20, 20, 20));
	ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 0, 360, NULL);
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 370 - i*2, 10, NULL);
	}
	
// ==================== 渐变效果环 ====================
	for(int i = 0; i <= 30; i++) {
		const float cr = radius + i * 0.14f * scale;
		setcolor(EGERGB(60+i, 60+i, 60+i));
		setfillcolor(EGERGB(60+i, 60+i, 60+i));
		ege_fillpie(centerX - cr, centerY - cr, cr*2, cr*2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		const float cr = radius + i * 0.14f * scale;
		setcolor(EGERGB(60+i, 60+i, 60+i));
		setfillcolor(EGERGB(60+i, 60+i, 60+i));
		ege_fillpie(centerX - cr, centerY - cr, cr*2, cr*2, 370 - i*2, 10, NULL);
	}
	
// 中心环（改为fillcircle）
	setcolor(EGERGB(10, 10, 10));
	setfillcolor(EGERGB(10, 10, 10));
	ege_fillcircle(centerX, centerY, radius + 2 * scale);  // 替换原sector
	
// 最内环（改为fillcircle）
	setcolor(EGERGB(35, 36, 35));
	setfillcolor(EGERGB(35, 36, 35));
	ege_fillcircle(centerX, centerY, radius);  // 替换原sector
}


void drawScaleNum(int centerX, int centerY, int radius)
{
	// 预计算数字位置
	int numPos[12][2] = {0};
	for (int i = 0; i < 12; i++) 
	{
		double angle = 90 - i * 30;
		double rad = angle * PI / 180;
		numPos[i][0] = centerX + (radius - radius*0.233) * cos(rad) - radius*0.053;
		numPos[i][1] = centerY - (radius - radius*0.233) * sin(rad) - radius*0.08;
	}
	// 绘制刻度
	for (int i = 0; i < 60; i++) 
	{
		double angle = 90 - i * 6;
		double rad = angle * PI / 180;
		int len = (i % 5 == 0) ? radius*0.133 : radius*0.067;
		setcolor(WHITE);
		setlinestyle(SOLID_LINE,0,radius*0.014);
		ege_line(
			centerX + (radius - len) * cos(rad),
			centerY - (radius - len) * sin(rad),
			centerX + radius * cos(rad),
			centerY - radius * sin(rad));
	}	
	// 绘制数字
	setbkmode(TRANSPARENT);  // 设置文字背景透明	
	setcolor(WHITE);
	setfont(radius/6, 0, "SimHei");
	for (int i = 0; i < 12; i++)
	{
		char str[3];
		sprintf(str, "%d", (i == 0) ? 12 : i);
		xyprintf(numPos[i][0], numPos[i][1], "%s", str);
	}	
}

void drawScrew(float x, float y, float r)
{	
//	// 螺丝参数化
	setfillcolor(EGERGB(65,65,65));
	setcolor(BLACK);
	setlinestyle(SOLID_LINE, 0, r*0.014);
<<<<<<< HEAD
	
=======

>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
	ege_fillellipse(x-r*0.667,y-r*0.02,r*0.093,r*0.093);
	ege_line(x-r*0.653,y+r*0.053,x-r*0.593,y);	
	ege_fillellipse(x+r*0.573,y-r*0.02,r*0.093,r*0.093);
	ege_line(x+r*0.646,y,x+r*0.593,y+r*0.053);		
}

void rotate_point(int cx, int cy, int x, int y, float angle, int* outX, int* outY)// 旋转坐标计算函数
{
	// 将点平移到原点
	x -= cx;
	y -= cy;	
	// 计算旋转后的坐标
	*outX = cx + (int)(x * cos(angle) - y * sin(angle));
	*outY = cy + (int)(x * sin(angle) + y * cos(angle));
}

void drawPointer(int cx, int cy, int radius, float angle, float dAngle)// 绘制旋转指针
{
	// 计算比例因子 (基于原始设计为  半径的表盘)
	const float scale = radius;	
	// 指针各部分尺寸定义
	float pointer_width1 = 0.067 * scale;    // 指针宽度
	const float pointer_length1 = 0.667 * scale;  // 指针总长度
	const float pointer_tip_length1 = 0.12 * scale;  // 指针尖端超出主体的长度
<<<<<<< HEAD
	
	// 指针各部分坐标（比例化后）
	// 指针主体矩形坐标
	int rect_left1 = cx - static_cast<int>(pointer_width1/2);
	int rect_top1 = cy - static_cast<int>(pointer_length1 - pointer_tip_length1) + radius / 20 - 1;
	int rect_right1 = cx + static_cast<int>(pointer_width1/2);
	int rect_bottom1 = cy - static_cast<int>(pointer_tip_length1) + radius / 20;
	// 指针三角形坐标
	int tri_points1[3][2] = 
=======
	
	// 指针各部分坐标（比例化后）
	// 指针主体矩形坐标
	int rect_left1 = cx - static_cast<int>(pointer_width1/2);
	int rect_top1 = cy - static_cast<int>(pointer_length1 - pointer_tip_length1) + radius / 20 - 1;
	int rect_right1 = cx + static_cast<int>(pointer_width1/2);
	int rect_bottom1 = cy - static_cast<int>(pointer_tip_length1) + radius / 20;
	// 指针三角形坐标
	int tri_points1[3][2] = 
	{
		{cx - static_cast<int>(pointer_width1/2), cy - static_cast<int>(pointer_length1 - pointer_tip_length1) + radius / 20},
		{cx + static_cast<int>(pointer_width1/2), cy - static_cast<int>(pointer_length1 - pointer_tip_length1) + radius / 20} ,
		{cx, cy - static_cast<int>(pointer_length1) + radius / 20} 
	};
	
	// 旋转后的坐标
	int rotated_rect1[4][2];
	int rotated_tri1[3][2];
	
	// 计算旋转后的矩形坐标
	rotate_point(cx, cy, rect_left1, rect_top1, angle + dAngle / 60, &rotated_rect1[0][0], &rotated_rect1[0][1]);
	rotate_point(cx, cy, rect_right1, rect_top1, angle + dAngle / 60, &rotated_rect1[1][0], &rotated_rect1[1][1]);
	rotate_point(cx, cy, rect_right1, rect_bottom1, angle + dAngle / 60, &rotated_rect1[2][0], &rotated_rect1[2][1]);
	rotate_point(cx, cy, rect_left1, rect_bottom1, angle + dAngle / 60, &rotated_rect1[3][0], &rotated_rect1[3][1]);
	
	// 计算旋转后的三角形坐标
	for (int i = 0; i < 3; i++)
	{
		rotate_point(cx, cy, tri_points1[i][0], tri_points1[i][1], angle + dAngle / 60, 
			&rotated_tri1[i][0], &rotated_tri1[i][1]);
	}
	
	// 绘制指针主体（白色部分）
	setfillcolor(WHITE);
	setcolor(WHITE);
	ege_point rect_points1[4];
	for (int i = 0; i < 4; i++)
	{
		rect_points1[i].x = rotated_rect1[i][0];
		rect_points1[i].y = rotated_rect1[i][1];
	}
	ege_fillpoly(4, rect_points1);
	
	// 绘制指针尖端（三角形）
	ege_point tri_points_ege1[3];
	for (int i = 0; i < 3; i++)
	{
		tri_points_ege1[i].x = rotated_tri1[i][0];
		tri_points_ege1[i].y = rotated_tri1[i][1];
	}
	ege_fillpoly(3, tri_points_ege1);
	// 指针各部分尺寸定义
	float pointer_width = 0.08 * scale;    // 指针宽度
	const float pointer_length = 0.93 * scale;  // 指针总长度
	const float pointer_tip_length = 0.167 * scale;  // 指针尖端超出主体的长度	
	// 指针各部分坐标（比例化后）
	// 指针主体矩形坐标
	int rect_left = cx - static_cast<int>(pointer_width/2);
	int rect_top = cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20;
	int rect_right = cx + static_cast<int>(pointer_width/2);
	int rect_bottom = cy - static_cast<int>(pointer_tip_length) + radius / 20;
	// 指针三角形坐标
	int tri_points[3][2] = 
	{
		{cx - static_cast<int>(pointer_width/2), cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20+1},
		{cx + static_cast<int>(pointer_width/2), cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20+1} ,
		{cx, cy - static_cast<int>(pointer_length) + radius / 20+1} 
	};
	
	// 旋转后的坐标
	int rotated_rect[4][2];
	int rotated_tri[3][2];
	
	// 计算旋转后的矩形坐标
	rotate_point(cx, cy, rect_left, rect_top, angle + dAngle / 3600, &rotated_rect[0][0], &rotated_rect[0][1]);
	rotate_point(cx, cy, rect_right, rect_top, angle + dAngle / 3600, &rotated_rect[1][0], &rotated_rect[1][1]);
	rotate_point(cx, cy, rect_right, rect_bottom, angle + dAngle / 3600, &rotated_rect[2][0], &rotated_rect[2][1]);
	rotate_point(cx, cy, rect_left, rect_bottom, angle + dAngle / 3600, &rotated_rect[3][0], &rotated_rect[3][1]);
	
	// 计算旋转后的三角形坐标
	for (int i = 0; i < 3; i++)
	{
		rotate_point(cx, cy, tri_points[i][0], tri_points[i][1], angle + dAngle / 3600, 
			&rotated_tri[i][0], &rotated_tri[i][1]);
	}
	
	// 绘制指针主体（白色部分）
	setfillcolor(WHITE);
	setcolor(WHITE);
	ege_point rect_points[4];
	for (int i = 0; i < 4; i++)
	{
		rect_points[i].x = rotated_rect[i][0];
		rect_points[i].y = rotated_rect[i][1];
	}
	ege_fillpoly(4, rect_points);
	
	// 绘制指针尖端（三角形）
	ege_point tri_points_ege[3];
	for (int i = 0; i < 3; i++)
	{
		tri_points_ege[i].x = rotated_tri[i][0];
		tri_points_ege[i].y = rotated_tri[i][1];
	}
	ege_fillpoly(3, tri_points_ege);

	// 绘制指针底部（深色部分）
	const float scale1 = radius / 240.0f;
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillcircle(cx, cy,  30 * scale1);  
	
	
	for(int i = 0; i <= 45; i++) 
>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
	{
		setcolor(EGERGB(30+i, 30+i, 30+i));
		setfillcolor(EGERGB(30+i, 30+i, 30+i));
		ege_fillpie(cx - 30 * scale1,cy - 30 * scale1,
			60 * scale1,60 * scale1,               
			90 + i*2,10,NULL);
	}

	for(int i = 0; i <= 45; i++) 
	{
		setcolor(EGERGB(30+i, 30+i, 30+i));
		setfillcolor(EGERGB(30+i, 30+i, 30+i));
		ege_fillpie(
			cx - 30 * scale1,cy - 30 * scale1,
			60 * scale1,60 * scale1,
			260 -i*2,10,NULL);
	}
	
<<<<<<< HEAD
	// 绘制指针主体（白色部分）
	setfillcolor(WHITE);
	setcolor(WHITE);
	ege_point rect_points1[4];
	for (int i = 0; i < 4; i++)
	{
		rect_points1[i].x = rotated_rect1[i][0];
		rect_points1[i].y = rotated_rect1[i][1];
	}
	ege_fillpoly(4, rect_points1);
	
	// 绘制指针尖端（三角形）
	ege_point tri_points_ege1[3];
	for (int i = 0; i < 3; i++)
	{
		tri_points_ege1[i].x = rotated_tri1[i][0];
		tri_points_ege1[i].y = rotated_tri1[i][1];
	}
	ege_fillpoly(3, tri_points_ege1);
	// 指针各部分尺寸定义
	float pointer_width = 0.08 * scale;    // 指针宽度
	const float pointer_length = 0.93 * scale;  // 指针总长度
	const float pointer_tip_length = 0.167 * scale;  // 指针尖端超出主体的长度	
	// 指针各部分坐标（比例化后）
	// 指针主体矩形坐标
	int rect_left = cx - static_cast<int>(pointer_width/2);
	int rect_top = cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20;
	int rect_right = cx + static_cast<int>(pointer_width/2);
	int rect_bottom = cy - static_cast<int>(pointer_tip_length) + radius / 20;
	// 指针三角形坐标
	int tri_points[3][2] = 
	{
		{cx - static_cast<int>(pointer_width/2), cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20+1},
		{cx + static_cast<int>(pointer_width/2), cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20+1} ,
		{cx, cy - static_cast<int>(pointer_length) + radius / 20+1} 
	};
	
	// 旋转后的坐标
	int rotated_rect[4][2];
	int rotated_tri[3][2];
	
	// 计算旋转后的矩形坐标
	rotate_point(cx, cy, rect_left, rect_top, angle + dAngle / 3600, &rotated_rect[0][0], &rotated_rect[0][1]);
	rotate_point(cx, cy, rect_right, rect_top, angle + dAngle / 3600, &rotated_rect[1][0], &rotated_rect[1][1]);
	rotate_point(cx, cy, rect_right, rect_bottom, angle + dAngle / 3600, &rotated_rect[2][0], &rotated_rect[2][1]);
	rotate_point(cx, cy, rect_left, rect_bottom, angle + dAngle / 3600, &rotated_rect[3][0], &rotated_rect[3][1]);
	
	// 计算旋转后的三角形坐标
	for (int i = 0; i < 3; i++)
	{
		rotate_point(cx, cy, tri_points[i][0], tri_points[i][1], angle + dAngle / 3600, 
			&rotated_tri[i][0], &rotated_tri[i][1]);
	}
	
	// 绘制指针主体（白色部分）
	setfillcolor(WHITE);
	setcolor(WHITE);
	ege_point rect_points[4];
	for (int i = 0; i < 4; i++)
	{
		rect_points[i].x = rotated_rect[i][0];
		rect_points[i].y = rotated_rect[i][1];
	}
	ege_fillpoly(4, rect_points);
	
	// 绘制指针尖端（三角形）
	ege_point tri_points_ege[3];
	for (int i = 0; i < 3; i++)
	{
		tri_points_ege[i].x = rotated_tri[i][0];
		tri_points_ege[i].y = rotated_tri[i][1];
	}
	ege_fillpoly(3, tri_points_ege);
	
	// 绘制指针底部（深色部分）
	const float scale1 = radius / 240.0f;
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillcircle(cx, cy,  30 * scale1);  
	
	
	for(int i = 0; i <= 45; i++) 
	{
		setcolor(EGERGB(30+i, 30+i, 30+i));
		setfillcolor(EGERGB(30+i, 30+i, 30+i));
		ege_fillpie(cx - 30 * scale1,cy - 30 * scale1,
			60 * scale1,60 * scale1,               
			90 + i*2,10,NULL);
	}
	
	for(int i = 0; i <= 45; i++) 
	{
		setcolor(EGERGB(30+i, 30+i, 30+i));
		setfillcolor(EGERGB(30+i, 30+i, 30+i));
		ege_fillpie(
			cx - 30 * scale1,cy - 30 * scale1,
			60 * scale1,60 * scale1,
			260 -i*2,10,NULL);
	}
	
=======
>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillcircle(cx, cy,  20 * scale1); 	
	
	//绘制秒针
	int secondHand[2];
	setlinestyle(SOLID_LINE, 0, 1);
	setcolor(WHITE);
	rotate_point(cx, cy, cx - radius*0.85, cy, angle + dAngle, &secondHand[0], &secondHand[1]);
	ege_line(cx, cy, secondHand[0], secondHand[1]);
	
}

void drawDashboard(int centerX, int centerY, int radius, float angle, float dAngle)
{
	drawBaseDashboard(centerX, centerY, radius);
	drawScaleNum(centerX, centerY, radius);
	drawScrew(centerX, centerY, radius);
	drawPointer(centerX, centerY, radius, angle, dAngle);
}

int main() 
{
	initgraph(800, 600, INIT_RENDERMANUAL);
	setbkcolor(RGB(190,190,190));
	setfillcolor(BLACK);	
	ege_enable_aa(true);
	setrendermode(RENDER_MANUAL);
	
	float centerX = 300;
	float centerY = 200;
	float radius = 100;
	float angle = 0, dAngle = 0;
	while (!kbhit()) 
	{
		cleardevice();
		// 绘制表盘
		drawDashboard(centerX, centerY, radius, angle, dAngle);	
		//更新增加角度
		dAngle +=  5 * PI / 180;
		// 控制帧率
		delay_jfps(16);
	}	
	closegraph();
	return 0;
}